﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinToTray
{
    public partial class FrmMain : Form
    {
        const int HOTKEY_EXIT = 100;
        const int HOTKEY_RESTOREALL = 101;
        const int HOTKEY_CLOSEALL = 102;
        const int HOTKEY_MINCURRENT = 103;
        uint WM_SHELLHOOK = 0;
          
        public FrmMain()
        {
            InitializeComponent();
        }
         
        private void UnHookAndExit()
        {
            Win32.DeregisterShellHookWindow(this.Handle);

            MouseLLHook.Stop();

            Win32.UnHook();
            //注销热键设定
            HotKeyMgr.UnregisterHotKey(Handle, HOTKEY_EXIT);
            HotKeyMgr.UnregisterHotKey(Handle, HOTKEY_RESTOREALL);
            HotKeyMgr.UnregisterHotKey(Handle, HOTKEY_CLOSEALL);
            HotKeyMgr.UnregisterHotKey(Handle, HOTKEY_MINCURRENT);
            //恢复全部窗口
            TrayMgr.ResotreAllWindowWithoutAuth();
            Environment.Exit(0);
        }

        private void ProcessHwndCreated(IntPtr hWnd)
        { 
            Win32.GetWindowThreadProcessId(hWnd, out var pId);
            Process p = Process.GetProcessById(pId);
            
            if(p.ProcessName == "Wow-64")
            {
                Task.Factory.StartNew(() => {
                    Thread.Sleep(1000);
                    Win32.SetWindowText(hWnd, "无标题");
                    int WM_SETICON = 0x80;
                    int ICON_SMALL = 0;
                    int ICON_BIG = 1;
                    var icon = Properties.Resources.ico1;
                    Win32.SendMessage(hWnd, WM_SETICON, ICON_BIG, icon.Handle);
                    Win32.SendMessage(hWnd, WM_SETICON, ICON_SMALL, icon.Handle);
                });
            }
             
            //int length = PInvoke.GetWindowTextLength(hWnd);
            //StringBuilder sb = new StringBuilder(length + 1);
            //PInvoke.GetWindowText(hWnd, sb, sb.Capacity);
            //if(sb.ToString() == PInvoke.WOW_TTITLE)
            //{
            
            //}
             
            return;
        }
         
        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WinMsg.WM_ADDTRAY:
                    ProcessHwndCreated((IntPtr)m.LParam);
                    TrayMgr.MinWindowToTray((IntPtr)m.LParam);
                    break;
                case WinMsg.WM_REFRTRAY:
                    TrayMgr.RemoveTrayIfNeedless((IntPtr)m.LParam); 
                    break;
                case WinMsg.WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case HOTKEY_EXIT: 
                            UnHookAndExit();
                            break;
                        case HOTKEY_RESTOREALL:
                            TrayMgr.ResotreAllWindowWithoutAuth();
                            break;
                        case HOTKEY_CLOSEALL:
                            TrayMgr.CloseAllWindowWithoutAuth();
                            break;
                        case HOTKEY_MINCURRENT:
                            TrayMgr.MinForegroundWindowToTray();
                            break;
                    }
                    break;
               

                default:
                    break;
            }


            if (m.Msg == WM_SHELLHOOK)
            {   // Receive shell messages
                switch ((ShellEvents)m.WParam.ToInt32())
                {
                    case ShellEvents.HSHELL_WINDOWCREATED:
                        ProcessHwndCreated(m.LParam);
                        break;
                }
            }

            base.WndProc(ref m);
        }
         
        private void FrmMain_Load(object sender, EventArgs e)
        {
            Text = Win32.MinToTrayWindowName;//for HookLib FindWindow
            Win32.InitHook();
            MouseLLHook.Start();
            //注册热键
            HotKeyMgr.RegisterHotKey(Handle, HOTKEY_EXIT, HotKeyMgr.KeyModifiers.Ctrl|HotKeyMgr.KeyModifiers.Alt|HotKeyMgr.KeyModifiers.Shift, Keys.Z);
            HotKeyMgr.RegisterHotKey(Handle, HOTKEY_RESTOREALL, HotKeyMgr.KeyModifiers.Ctrl | HotKeyMgr.KeyModifiers.Alt | HotKeyMgr.KeyModifiers.Shift, Keys.R);
            HotKeyMgr.RegisterHotKey(Handle, HOTKEY_CLOSEALL, HotKeyMgr.KeyModifiers.Ctrl | HotKeyMgr.KeyModifiers.Alt | HotKeyMgr.KeyModifiers.Shift, Keys.C);
            HotKeyMgr.RegisterHotKey(Handle, HOTKEY_MINCURRENT, HotKeyMgr.KeyModifiers.Ctrl | HotKeyMgr.KeyModifiers.Alt | HotKeyMgr.KeyModifiers.Shift, Keys.X);

            WM_SHELLHOOK = Win32.RegisterWindowMessage("SHELLHOOK");
            Win32.RegisterShellHookWindow(this.Handle);
        }
         
        protected override CreateParams CreateParams
        {
            get
            {
                const int WS_EX_APPWINDOW = 0x40000;
                const int WS_EX_TOOLWINDOW = 0x80;
                const int WS_VISIBLE = 0x10000000;
                CreateParams cp = base.CreateParams;
               
                cp.ExStyle &= (~WS_EX_APPWINDOW);    // 不显示在TaskBar
                cp.ExStyle &= (~WS_VISIBLE);        // 隐藏
                cp.ExStyle |= WS_EX_TOOLWINDOW;      // 不显示在Alt+Tab
                return cp;
            }
        }

     
    }
}

